package com.housekeeping.housekeeping.advertisingmanagement.controller.ad;/*
 * @author  pyh
 * @date  2021/1/21 0021 上午 9:51
 * @version 1.0
 * @since JDK1.8_241
 */

import com.housekeeping.housekeeping.advertisingmanagement.entity.AdMessages;
import com.housekeeping.housekeeping.advertisingmanagement.service.AdMessagesService;
import com.housekeeping.housekeeping.advertisingmanagement.service.impl.AdMessagesServiceImpl;
import com.housekeeping.housekeeping.advertisingmanagement.service.impl.SearchServiceImpl;
import com.housekeeping.housekeeping.advertisingmanagement.utils.Page;
import com.housekeeping.housekeeping.advertisingmanagement.utils.ResponseEntitys;
import com.housekeeping.housekeeping.advertisingmanagement.utils.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


/**
 * 模糊查询
 * 这里把entity = ResponseEntitys.success(list);转了对象，如果其他地方出错了，找这里。
 */
@WebServlet("/ad/fuzzy")
public class FuzzyController extends HttpServlet {
    private AdMessagesService ams = new AdMessagesServiceImpl();
    //private SearchServiceImpl ssi = new SearchServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取输入框的广告名称,和下拉框的状态
        String name=req.getParameter("name");
        String status = req.getParameter("status");
        //分页需要的两个参数
        int page = Integer.parseInt(req.getParameter("page"));
        int size = Integer.parseInt(req.getParameter("size"));
        Page<List<AdMessages>> objectPage = new Page<>();
        List<AdMessages> list = new ArrayList<>();
        ResponseEntitys<List<AdMessages>> entity = null;
        try {
            if (name == null || name.equals("")){
                //传递名称
                List<AdMessages> adMessagesList = ams.getAdMessagesList(name);
                entity = ResponseEntitys.success(adMessagesList);
            }else if (name != null && !name.equals("")){
                //传递广告名称和状态值
                List<AdMessages> pAndSList = ams.getAdMessagesList(name);
                entity = ResponseEntitys.success(pAndSList);
            }else{
                entity = ResponseEntitys.error(40007,"模糊查询有误");
            }
/*            List<AdMessages> adMessagesList = ams.getAdMessagesList(name);
            if (adMessagesList != null){
                list.add((AdMessages) adMessagesList);
                objectPage.setData(Collections.singletonList(list));
                entity = ResponseEntitys.success(list);
            }*/
        } catch (SQLException throwables) {
            entity = ResponseEntitys.error(40007,"模糊查询有误");
        }
        ResponseUtils.resp(resp,entity);
    }
}
